package com.fingard.dsp.bank.directbank.bocom01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ICancelAuthorizeForRec;
import com.fingard.dsp.bank.reqrespobj.authcancelforrec.*;
import com.fingard.dsp.bank.reqrespobj.authcancelforrec.detail.RetAtsQryRecAuthCancelInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOCOM01AuthorizeCancelForRec extends BOCOM01Base implements ICancelAuthorizeForRec {
    @Override
    public RetAtsAuthCancelForRec d_AuthCancel(ReqAuthCancelForRec p_req) throws Exception {
        RetAtsAuthCancelForRec tmpRetAts = new RetAtsAuthCancelForRec(p_req.reqHeader);
        try{
            tr_code = "FHTS01";
            String transCode = "400101";
            String contractNo = getBankConfig().getMapValueByKey("cagrNo" + transCode, p_req.ownAct);
            //暂时不支持批量
            if(p_req.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
                // 写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("transCode", transCode);//交易码
                xmlWriter.writeElementString("subdate", Format.Date8Format.format(new Date()));//委托日期 格式YYYYMMDD
                xmlWriter.writeElementString("outid", p_req.reqSeqID);//交易序号
                xmlWriter.writeElementString("contracttype", "0");//协议标识  0-收,1-付 （目前只提供代收业务）
                xmlWriter.writeElementString("chgtype", "102");//协议操作标识 100-增加，101-变更 ， 102-撤销
                xmlWriter.writeElementString("sndOrgNo", bankFront.org);//收费机构代码
                xmlWriter.writeElementString("sndBranchNo", "");//机构分支代码
                xmlWriter.writeElementString("contractno", contractNo);//协议号 变更时必输
                xmlWriter.writeElementString("custno", getValueAsString("custno"));//用户编号
                xmlWriter.writeElementString("cusNme", getValueAsString("cusNme"));//用户名称
                xmlWriter.writeElementString("payername", "");//联系人名称 变更时必输
                xmlWriter.writeElementString("addr", "");//联系人地址 变更时必输
                xmlWriter.writeElementString("contZipCde", "");//联系人地址邮编 变更时必输
                xmlWriter.writeElementString("tel", "");//联系人电话 变更时必输
                xmlWriter.writeElementString("busKnd", getBankConfig().getMapValueByKey("PayType", p_req.payType));//业务种类
                xmlWriter.writeElementString("bankno", "");//付款行行号
                xmlWriter.writeElementString("acctno", p_req.readedDetail.oppAct);//付款人账号
                xmlWriter.writeElementString("acctname", p_req.readedDetail.oppActName);//付款人名称
                xmlWriter.writeElementString("payeridtype", convertCertTypeToBank(p_req.readedDetail.certType));//付款人证件类型
                xmlWriter.writeElementString("payerid", p_req.readedDetail.certNum);//付款人证件号码
                xmlWriter.writeElementString("payTel", "");//付款人手机号码 变更时必输
                xmlWriter.writeElementString("payEmail", "");//付款人电子邮箱 变更时必输
                xmlWriter.writeElementString("signDate", Format.Date8Format.format(new Date()));//签约起始日期 格式YYYYMMDD
                xmlWriter.writeElementString("signEndDate", p_req.readedDetail.deadLine);//到期日期 格式YYYYMMDD
                xmlWriter.writeElementString("desc", "");//附言
                xmlWriter.writeElementString("remark", "");//备注
                xmlWriter.writeEndDocument();
                String[] bankResult = sendToBank(xmlWriter.toXmlString());
                if(bankResult[0].length()==0){
                    StringBuilder tmpSbMsg =new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                    if(tmpBkReader!=null){
                        tmpRetAts.respCode = "0";
                        String ans_code =tmpBkReader.getFirstText("/ap/head/ans_code");
                        String responseCode = tmpBkReader.getFirstText("/ap/body/responseCode");
                        String ans_info =tmpBkReader.getFirstText("/ap/head/ans_info");
                        if("0".equals(ans_code)){
                            if("000000".equals(responseCode)){
                                tmpRetAts.retCode = "0";
                                tmpRetAts.serialNo =tmpBkReader.getFirstText("/ap/body/transno");
                            }else{
                                tmpRetAts.retCode = "1";
                            }
                            tmpRetAts.retMsg = tmpBkReader.getFirstText("/ap/body/responseMessage");
                        }else{
                            tmpRetAts.retCode = "2";
                            tmpRetAts.retMsg = ans_info;
                        }
                        tmpRetAts.reqSeqID =p_req.reqSeqID;
                    }else{
                        tmpRetAts.respCode="-1";
                        tmpRetAts.respInfo=tmpSbMsg.toString();
                    }

                }else{
                    tmpRetAts.respCode = "-1";
                    tmpRetAts.respInfo = bankResult[1];
                }
            }else {
                tmpRetAts.respCode = "-1";
                tmpRetAts.respInfo = "读取detail为空";
            }
        }catch (Exception ex){
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        tmpRetAts.writeRetToATS();
        return tmpRetAts;
    }

    @Override
    public RetAtsQryAuthCancelForRec d_qry_AuthCancel(ReqQryAuthCancelForRec p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsQryRecAuthCancelInfo d_qry_AuthCancelInfo(ReqQryRecAuthCancelInfo p_req) throws Exception {
        RetAtsQryRecAuthCancelInfo tmpRetAts = new RetAtsQryRecAuthCancelInfo(p_req.reqHeader);
        try {
            tr_code = "FHTS01";
            String transCode = "200101";
            String contractNo = getBankConfig().getMapValueByKey("cagrNo" + transCode, p_req.ownAct);//有问题

            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            // 写入通用的头部节点
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("body");
            xmlWriter.writeElementString("transCode", transCode);//交易码
            xmlWriter.writeElementString("sndOrgNo", bankFront.org);//收费机构代码
            xmlWriter.writeElementString("sndBranchNo","");//机构分支代码
            xmlWriter.writeElementString("contractno",contractNo);//合同号
            xmlWriter.writeElementString("custno",getValueAsString("custno"));//用户编号
            xmlWriter.writeElementString("cusNme",getValueAsString("cusNme"));//用户名称
            xmlWriter.writeElementString("busKnd","");//业务种类
            xmlWriter.writeElementString("bankno","");//付款行行号
            xmlWriter.writeElementString("acctno",p_req.oppAct);//付款人账号
            xmlWriter.writeElementString("acctname",p_req.oppName);//付款人名称
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            tmpRetAts.ownAct = p_req.ownAct;
            XmlTextReader tmpBkReader;
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String ans_code = tmpBkReader.getFirstText("/ap/head/ans_code");
                    String responseCode = tmpBkReader.getFirstText("/ap/body/responseCode");
                    if("0".equals(ans_code)){
                        if("000000".equals(responseCode)){
                            tmpRetAts.respCode = "0";
                            RetAtsQryRecAuthCancelInfoDetail detail = new RetAtsQryRecAuthCancelInfoDetail();
                            detail.oppAct =tmpBkReader.getFirstText("/ap/body/acctno");
                            detail.oppActName =tmpBkReader.getFirstText("/ap/body/acctname");
                            detail.authDateFrom =tmpBkReader.getFirstText("/ap/body/signDate"); //格式YYYYMMDD
                            detail.queryTag ="0".equals(tmpBkReader.getFirstText("/ap/body/status"))?"1":"0"; //协议状态 0-无效,1-人行或者本地已签订,2-已激活
                            detail.respReserve1 ="";
                            detail.respReserve2 ="";
                            detail.respReserve3 ="";
                            tmpRetAts.writeRetDetail(detail);
                        }else {
                            tmpRetAts.respCode = "-1";
                        }
                        tmpRetAts.respInfo = tmpBkReader.getFirstText("/ap/body/responseMessage");

                    }else{
                        tmpRetAts.respCode = "-1";
                        tmpRetAts.respInfo = tmpBkReader.getFirstText("/ap/head/ans_info");
                    }
                } else {
                    tmpRetAts.respCode = "-1";
                    tmpRetAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAts.respCode = "-1";
                tmpRetAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception ex) {
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        tmpRetAts.writeAfterDetail();
        return tmpRetAts;
    }
}
